package com.dr.cmdb.core.mongodao.impl;

import com.dr.cmdb.core.mongodao.MongoTempleDao;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: cmdb
 * @Package: com.dr.cmdb.application.mongoDao.impl
 * @Description: java类作用描述
 * @Author: duanrui
 * @CreateDate: 2021/3/17 9:55
 * @Version: 1.0
 * <p>
 * Copyright: Copyright (c) 2021
 */
@Component
public class MongoTempleDaoImpl implements MongoTempleDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Object obj, String collName) {
        mongoTemplate.save(obj, collName);
    }

    /**
     * 修改
     *
     * @param params
     * @param collName
     */
    @Override
    public void edit(Map<String, Object> params, String collName) {
        Query query = new Query(Criteria.where("_id").is(params.get("_id")));
        Update update = new Update();
        params.forEach(
                update::set
        );
        mongoTemplate.updateFirst(query, update, collName);
    }


    /**
     * 删除
     *
     * @param code
     * @param collName
     */
    @Override
    public void delete(String code, String collName) {
        Query query = new Query(Criteria.where("_id").is(code));
        mongoTemplate.remove(query, collName);
    }

    /**
     * 批量保存
     *
     * @param list
     * @param collName
     */
    @Override
    public void saveList(List<Map<String, Object>> list, String collName) {
        mongoTemplate.insert(list, collName);
    }

    /**
     * 查询列表
     *
     * @param map
     * @return
     */

    @Override
    public List<Object> getListByParams(Map<String, Object> map, String collName) {
        Query query = new BasicQuery(new Document(map));
        return mongoTemplate.find(query, Object.class, collName);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public List<Object> findAll(String collName) {
        return mongoTemplate.findAll(Object.class, collName);
    }


    /**
     * 新增模型属性
     *
     * @param collName
     * @param propertiesMap k为新增字段名，v为新增字段值
     */
    @Override
    public void addProperties(String collName, String tableName, Map<String, Object> propertiesMap) {
        Query query = new Query(Criteria.where("tableName").is(tableName));
        Update update = new Update();
        // 新增一个字段
        propertiesMap.forEach(
                update::addToSet
        );
        mongoTemplate.updateMulti(query, update, collName);
    }

    /**
     * 修改模型属性
     *
     * @param collName
     * @param properties
     */
    @Override
    public void delProperties(String collName, String tableName, List<String> properties) {
        Query query = new Query(Criteria.where("tableName").is(tableName));
        Update update = new Update();
        // 新增一个字段
        properties.forEach(
                update::unset
        );
        mongoTemplate.updateMulti(query, update, collName);
    }

    /**
     * 更改模型属性
     *
     * @param collName
     * @param propertiesMap
     */
    @Override
    public void updateProperties(String collName, String tableName, Map<String, String> propertiesMap) {
        Query query = new Query(Criteria.where("tableName").is(tableName));
        Update update = new Update();
        // 新增一个字段
        propertiesMap.forEach(
                update::rename
        );
        mongoTemplate.updateMulti(query, update, collName);

    }
}
